<template>
  <div id="app">
    <div class="container">
      <div class="item" v-for="(item, index) in items" :key="index">
        <img :src="item.image" :alt="item.alt" />
        <div class="buttons">
          <button @click="previewFile(item.file)">预览</button>
          <a :href="item.file"><button>下载</button></a>
        </div>
      </div>
    </div>
    <div v-if="showPreview" class="pdf-preview">
      <vue-pdf-embed :source="currentPdf" />
      <button @click="closePreview">关闭预览</button>
    </div>
  </div>
</template>

<script setup>
import { ref } from "vue";
import VuePdfEmbed from "vue-pdf-embed";
import cocoImg from "./assets/coco.png";
import haierImg from "./assets/haier.png";
import teaImg from "./assets/tea.png";
const showPreview = ref(false);
const currentPdf = ref(null);

const items = ref([
  { image: teaImg, alt: "Tea", file: "/pdf/pdf1.pdf" },
  { image: cocoImg, alt: "Coco", file: "/pdf/pdf2.pdf" },
  { image: haierImg, alt: "Haier", file: "/pdf/pdf3.pdf" },
]);

const previewFile = (file) => {
  currentPdf.value = file;
  showPreview.value = true;
};

const closePreview = () => {
  showPreview.value = false;
  currentPdf.value = null;
};
</script>

<style>
.container {
  width: 100vw;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 20px;
  margin-bottom: 40px;
}

.item {
  background-color: #fff;
  border-radius: 12px;
  box-shadow: 0 8px 16px rgba(0, 0, 0, 0.1);
  transition: transform 0.2s;
  width: 400px;
  text-align: center;
  padding: 20px;
}

.item:hover {
  transform: translateY(-5px);
}

.item img {
  max-width: 100%;
  height: auto;
  border-radius: 8px;
  margin-bottom: 10px;
}

.buttons {
  display: flex;
  justify-content: space-around;
  margin-top: 10px;
}

button {
  padding: 16px 40px;
  background-color: #007bff;
  color: white;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-size: 16px;
  transition: background-color 0.2s;
}

a {
  display: inline-block;
}

button:hover {
  background-color: #0056b3;
}

.pdf-preview {
  width: 100vw;
  height: 100vh;
  position: fixed;
  top: 0;
  right: 0;
  left: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.7);
  display: flex;
  justify-content: center;
  align-items: center;
}

.vue-pdf-embed {
  height: 100vh;
  width: 90vw;
  overflow: auto;
}

canvas {
  width: 100%;
}

.pdf-preview button {
  user-select: none;
  position: absolute;
  top: 10px;
  right: 2px;
  background-color: #ff4d4f;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-size: 16px;
  padding: 10px 20px;
}
</style>
